<?php	
class Obj_Text {

	public static function removeHTMLComments($s) {
		return self::strip_word_html($s);
	}
	
    private static function strip_word_html($text, $allowed_tags = '<h1><h2><h3><h4><h5><h6><strike><font><a><b><i><sup><sub><em><strong><u><br><p><table><div><tr><td><th><tbody><thead><ul><li><ol><blockquote>')
    {
        mb_regex_encoding('UTF-8');
        //replace MS special characters first
        $search = array('/&lsquo;/u', '/&rsquo;/u', '/&ldquo;/u', '/&rdquo;/u', '/&mdash;/u');
        $replace = array('\'', '\'', '"', '"', '-');
        $text = preg_replace($search, $replace, $text);
        //make sure _all_ html entities are converted to the plain ascii equivalents - it appears
        //in some MS headers, some html entities are encoded and some aren't
        $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
        //try to strip out any C style comments first, since these, embedded in html comments, seem to
        //prevent strip_tags from removing html comments (MS Word introduced combination)
        if(mb_stripos($text, '/*') !== FALSE){
            $text = mb_eregi_replace('#/\*.*?\*/#s', '', $text, 'm');
        }
        //introduce a space into any arithmetic expressions that could be caught by strip_tags so that they won't be
        //'<1' becomes '< 1'(note: somewhat application specific)
        $text = preg_replace(array('/<([0-9]+)/'), array('< $1'), $text);
        $text = strip_tags($text, $allowed_tags);
        //eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one
        $text = preg_replace(array('/^\s\s+/', '/\s\s+$/', '/\s\s+/u'), array('', '', ' '), $text);
        //strip out inline css and simplify style tags
        $search = array('#<(strong|b)[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu');
        $replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>');
        $text = preg_replace($search, $replace, $text);
        //on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears
        //that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains
        //some MS Style Definitions - this last bit gets rid of any leftover comments */
        $num_matches = preg_match_all("/\<!--/u", $text, $matches);
        if($num_matches){
              $text = preg_replace('/\<!--(.)*--\>/isu', '', $text);
        }
        return $text;
    }	


	public static function intro($text, $length) {
		$s = trim(strip_tags($text));
		$i = $length;
		$n = strlen($s);
		while ($i < $n) {
			if (!ctype_space(mb_substr($s, $i, 1))) $i++;
			else break;
		}
		if ($i < $n) {
			return mb_substr($s, 0, $i).' ...';
		}
		else {
			return mb_substr($s, 0, $length).'...';
		}
	}

    public static function translit($text, $save_register = false){
        $length = mb_strlen($text, 'utf-8');
        if (!$save_register)
            $text = mb_strtolower($text, 'utf-8');
        $translit = '';
        for ($i=0; $i<=$length-1; $i++){
            $letter = iconv_substr($text, $i, 1, 'utf-8');
            switch ($letter){
                case 'а':
                    $translit .= 'a';
                    break;
                case 'б':
                    $translit .= 'b';
                    break;
                case 'в':
                    $translit .= 'v';
                    break;
                case 'г':
                    $translit .= 'g';
                    break;
                case 'д':
                    $translit .= 'd';
                    break;
                case 'е':
                    $translit .= 'e';
                    break;
                case 'ё':
                    $translit .= 'yo';
                    break;
                case 'ж':
                    $translit .= 'zh';
                    break;
                case 'з':
                    $translit .= 'z';
                    break;
                case 'и':
                    $translit .= 'i';
                    break;
                case 'й':
                    $translit .= 'j';
                    break;
                case 'к':
                    $translit .= 'k';
                    break;
                case 'л':
                    $translit .= 'l';
                    break;
                case 'м':
                    $translit .= 'm';
                    break;
                case 'н':
                    $translit .= 'n';
                    break;
                case 'о':
                    $translit .= 'o';
                    break;
                case 'п':
                    $translit .= 'p';
                    break;
                case 'р':
                    $translit .= 'r';
                    break;
                case 'с':
                    $translit .= 's';
                    break;
                case 'т':
                    $translit .= 't';
                    break;
                case 'у':
                    $translit .= 'u';
                    break;
                case 'ф':
                    $translit .= 'f';
                    break;
                case 'х':
                    $translit .= 'h';
                    break;
                case 'ц':
                    $translit .= 'c';
                    break;
                case 'ч':
                    $translit .= 'ch';
                    break;
                case 'ш':
                    $translit .= 'sh';
                    break;
                case 'щ':
                    $translit .= 'w';
                    break;
                case 'ы':
                    $translit .= 'y';
                    break;
                case 'э':
                    $translit .= 'je';
                    break;
                case 'ю':
                    $translit .= 'ju';
                    break;
                case 'я':
                    $translit .= 'ya';
                    break;
                case 'А':
                    $translit .= 'A';
                    break;
                case 'Б':
                    $translit .= 'B';
                    break;
                case 'В':
                    $translit .= 'V';
                    break;
                case 'Г':
                    $translit .= 'G';
                    break;
                case 'Д':
                    $translit .= 'D';
                    break;
                case 'Е':
                    $translit .= 'E';
                    break;
                case 'Ё':
                    $translit .= 'YO';
                    break;
                case 'Ж':
                    $translit .= 'ZH';
                    break;
                case 'З':
                    $translit .= 'Z';
                    break;
                case 'И':
                    $translit .= 'I';
                    break;
                case 'Й':
                    $translit .= 'J';
                    break;
                case 'К':
                    $translit .= 'K';
                    break;
                case 'Л':
                    $translit .= 'L';
                    break;
                case 'М':
                    $translit .= 'M';
                    break;
                case 'Н':
                    $translit .= 'N';
                    break;
                case 'О':
                    $translit .= 'O';
                    break;
                case 'П':
                    $translit .= 'P';
                    break;
                case 'Р':
                    $translit .= 'R';
                    break;
                case 'С':
                    $translit .= 'S';
                    break;
                case 'Т':
                    $translit .= 'T';
                    break;
                case 'У':
                    $translit .= 'U';
                    break;
                case 'Ф':
                    $translit .= 'F';
                    break;
                case 'Х':
                    $translit .= 'H';
                    break;
                case 'Ц':
                    $translit .= 'C';
                    break;
                case 'Ч':
                    $translit .= 'CH';
                    break;
                case 'Ш':
                    $translit .= 'SH';
                    break;
                case 'Щ':
                    $translit .= 'W';
                    break;
                case 'Ы':
                    $translit .= 'Y';
                    break;
                case 'Э':
                    $translit .= 'JE';
                    break;
                case 'Ю':
                    $translit .= 'JU';
                    break;
                case 'Я':
                    $translit .= 'YA';
                    break;
                case '1':
                    $translit .= '1';
                    break;
                case '2':
                    $translit .= '2';
                    break;
                case '3':
                    $translit .= '3';
                    break;
                case '4':
                    $translit .= '4';
                    break;
                case '5':
                    $translit .= '5';
                    break;
                case '6':
                    $translit .= '6';
                    break;
                case '7':
                    $translit .= '7';
                    break;
                case '8':
                    $translit .= '8';
                    break;
                case '9':
                    $translit .= '9';
                    break;
                case '0':
                    $translit .= '0';
                    break;
                case 'a':
                case 'b':
                case 'c':
                case 'd':
                case 'e':
                case 'f':
                case 'g':
                case 'h':
                case 'i':
                case 'j':
                case 'k':
                case 'l':
                case 'm':
                case 'n':
                case 'o':
                case 'p':
                case 'q':
                case 'r':
                case 's':
                case 't':
                case 'u':
                case 'v':
                case 'w':
                case 'x':
                case 'y':
                case 'z':
                case 'A':
                case 'B':
                case 'C':
                case 'D':
                case 'E':
                case 'F':
                case 'G':
                case 'H':
                case 'I':
                case 'J':
                case 'K':
                case 'L':
                case 'M':
                case 'N':
                case 'O':
                case 'P':
                case 'Q':
                case 'R':
                case 'S':
                case 'T':
                case 'U':
                case 'V':
                case 'W':
                case 'X':
                case 'Y':
                case 'Z':
                    $translit .= $letter;
                    break;
                case ' ':
                case '-':
                case '_':
                    $translit .= '_';
                    break;
                default:
                    break;
            }
        }
        $translit = preg_replace('/[\_]{2,}/', '_', $translit);
        return $translit;
    }
	
} 
